<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\model;
use app\api\model\Common;
use think\Db;
class Order extends Common {

    /**
     * 关联GPS表 一对一
     */
    public function ordergps() {
        return $this->hasOne('OrderGps', 'order_id', 'order_id');
    }

    /**
     * 乘客表 一对一
     */
    public function passenger(){
        return $this->hasOne('Passenger','passenger_id','passenger_id');
    }
    
    /**
     * 关联司机表 一对一
     */
    public function driver(){
        return $this->hasOne('Driver','driver_id','driver_id');
    }
    
    /**
     * 关联车辆表 一对一
     */
    public function car(){
        return $this->hasOne('Car','car_id','car_id');
    }
    
    /**
     * 关联费用表 一对一
     */
    public function orderfee(){
        return $this->hasOne('OrderFee','order_id','order_id');
    }
    

    public function receiveOrder($oid, $driver_id) {
        return $this->save(['order_state' => 4, 'driver_id' => $driver_id], ['order_id' => $oid, 'driver_id' => null]);
    }

    /**
     * 获取订单列表
     * @param int $driver_id 司机id
     * @param int $car_type_id 车型id
     * @param datetime $start_time 开始时间
     * @param datetime $end_time 结束时间
     * @param int $order_state 0:历史订单，1:当前订单
     */
    public function getOrderList($driver_id, $car_type, $start_time, $end_time, $order_state, $use_type, $order_type, $booking_type, $page = 1, $pageSize = 10) {

        $where['driver_id'] = $driver_id;

        if($order_state){
            $where['order_state'] = ['in',$order_state];
        }
        if($car_type){
            $where['car_type']    = ['in',$car_type];
        }
        if ($start_time) {
            $where['plan_time']  = ['egt', $start_time];
        }
        if ($end_time) {
            $where['plan_time']  = $start_time ? ['between',"$start_time,$end_time"] : ['elt', $end_time];
        }

        $orderField = 'order_id,order_state,order_type,use_type,booking_type,order_time,0 car_type_id';
        $gpsField   = 'from_addr departure,from_lon,from_lat,to_addr destination,to_lon,to_lat,ifnull(plan_time,call_time) plan_time';
        $count      = Db::view('order',[$orderField])->view('order_gps',[$gpsField],'order.order_id = order_gps.order_id','left')->where($where)->count();
        $data       = Db::view('order',[$orderField])->view('order_gps',[$gpsField],'order.order_id = order_gps.order_id','left')->where($where)->page($page,$pageSize)->order('order_id desc')->select();
        return [$count,$data];
    }

    /**
     * 获取附近发单
     * @param $type 订单类型
     * @param int $driver 司机id
     * @param int $page 页数
     * @param int $num 记录数
     * TODO 根据司机经纬度及查询类型查询附近乘客发单
     * @return array
     */
    public function getNearOrder($type=1,$page=1,$num=10,$driver_id=0){
        $time        = time() + ($type-1)*24*60*60;
        $start_time  = date('Y-m-d',$time)." 00:00:00";
        $end_time    = date('Y-m-d',$time)." 23:59:59";
        if($driver_id>0){
            // TODO 司机定位附近订单判断
            $orderField = 'order_id,order_state,order_type,use_type,booking_type,order_time,0 car_type_id,departure,destination';
            $gpsField   = 'from_addr,from_lon,from_lat,to_addr,to_lon,to_lat,plan_time';
            $result     = Db::view('order',[$orderField])
                ->view('order_gps',[$gpsField],'order.order_id = order_gps.order_id','left')
                ->where("order_state",5)
                ->where('driver_id',$driver_id)
                ->where("plan_time between  '$start_time' and '$end_time' ")
                ->page($page,$num)
                ->select();
            if(!empty($result)){
                return [count($result),$result];
            }
        }
        $orderField = 'order_id,order_state,order_type,use_type,booking_type,order_time,car_type_id,departure,destination';
        $gpsField   = 'from_addr,from_lon,from_lat,to_addr,to_lon,to_lat,plan_time';
        $result     = Db::view('order',[$orderField])
            ->view('order_gps',[$gpsField],'order.order_id = order_gps.order_id','left')
            ->where("order_state",4)
            ->where('driver_id',null)
            ->where("plan_time between  '$start_time' and '$end_time' ")
            ->page($page,$num)
            ->select();
        return [count($result),$result];
    }

    /**
     * 获取订单详情
     * @param int $order_id 订单id
     * @return array 订单详情
     */
    public function getOrderDetail($order_id) {
        $order                   = $this->get($order_id);
        $data['driver_id']       = $order['driver_id'];
        $data['passenger_name']  = $order['passenger_name'];
        $data['passenger_phone'] = $order['passenger_phone'];
        $data['from_lon']        = $order->ordergps ? $order->ordergps['from_lon'] : '';
        $data['from_lat']        = $order->ordergps ? $order->ordergps['from_lat'] : '';
        $data['from_addr']       = $order->ordergps ? $order->ordergps['from_addr'] : '';
        $data['to_lon']          = $order->ordergps ? $order->ordergps['to_lon'] : '';
        $data['to_lat']          = $order->ordergps ? $order->ordergps['to_lat'] : '';
        $data['to_addr']         = $order->ordergps ? $order->ordergps['to_addr'] : '';
        $data['use_time']        = $order->ordergps ? $order->ordergps['plan_time'] : '';
        $data['go_time']         = $order->ordergps ? $order->ordergps['go_time'] : '';
        $data['driver_money']    = $order->orderfee ? $order->orderfee['driver_money'] : 0;
        $data['order_state']     = $order['order_state'];
        // TODO 费用列表
        return $data;
    }
    
    public static function checkFinishParam($param){
        $data['wait_time'] = isset($param['wait_time']) ? $param['wait_time'] : 0;
        $data['wait_fee']  = isset($param['wait_fee'])  ? $param['wait_fee']  : 0;
        if(!isset($param['use_time'])){
            return [0,'累积时长不能为空'];
        }
        $data['use_time']   = $param['use_time'];
        $data['time_fee']   = isset($param['time_fee'])  ? $param['time_fee']  : 0;
        if(!isset($param['total_meter'])){
            return [0,'累积里程不能为空'];
        }
        $data['total_meter'] = $param['total_meter'];
        $data['meter_fee']   = isset($param['meter_fee'])  ? $param['meter_fee']  : 0;
        $data['low_time']    = isset($param['low_time'])   ? $param['low_time']  : 0;
        $data['low_fee']     = isset($param['low_fee'])   ? $param['low_fee']  : 0;
        $data['night_meter'] = isset($param['night_meter'])   ? $param['night_meter']  : 0;
        $data['night_fee']   = isset($param['night_fee'])   ? $param['night_fee']  : 0;
        $data['high_fee']    = isset($param['high_fee'])   ? $param['high_fee']  : 0;
        $data['toll']        = isset($param['toll'])   ? $param['toll']  : 0;
        $data['park_fee']    = isset($param['park_fee'])   ? $param['park_fee']  : 0;
        $data['other_fee']   = isset($param['other_fee'])   ? $param['other_fee']  : 0;
        if($data['other_fee']>0){
            return [0,'必须填写费用备注'];
        }
        $data['other_note']  = isset($param['other_note'])   ? $param['other_note']  : 0;
        if(!isset($param['money'])){
            return [0,'费用不能为空'];
        }
        $data['money']       = $param['money'];
        $data['order_state'] = 7;
        return [1,0,$data];
    }

    /**
     * @param int $oid
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getTrack($oid=1102){
        $order     = $this->get($oid);
        $driver_id = $order->getAttr('driver_id');
        $startTime = $order->getAttr('ride_time');
        $endTime   = $order->ordergps->getAttr('arrival_time');
        $url       = 'http://139.224.59.63:8080/ucar/manage/drivertrack/list.do?';
        $url       = $url.'driverId='.$driver_id.'&';
        $url       = $url.'startTime='.urlencode($startTime).'&';
        $url       = $url.'endTime='.urlencode($endTime).'&';
        $ch        = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
